<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1-5 基于闭包实现获取元素样式（惰性函数优化）</title>
  <style>
    .box {
      width: 200px;
      height: 200px;
    }
  </style>
</head>

<body>
  <div class="box">box...</div>

  <script>
    function getElemStyle(el, attr) {
      if (window.getComputedStyle) {
        getElemStyle = function getElemStyle() {
          return window.getComputedStyle(el)[attr];
        }
      } else {
        getElemStyle = function getElemStyle() {
          return el.currentStyle[attr];
        }
      }
      // 第一次进来也要执行一次
      return getElemStyle(el, attr);
    }
    var box_width = getElemStyle(document.querySelector('.box'), 'width');
    var box_height = getElemStyle(document.querySelector('.box'), 'height');
    console.log(box_width, box_height);
  </script>
</body>

</html>